 using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MySql;
using MySql.Data;
using MySql.Data.MySqlClient;
using MySql.Data.Entity;
using ASV.Classes;
using System.Data;
using System.Security.Cryptography;

namespace ASV.Account
{
    public partial class Benutzer : System.Web.UI.Page
    {
        protected void Page_Init(object sender, EventArgs e)
        {
            Authentication auth = new Authentication();
            int userGroup;
            int allowedGroup;

            string selectedID = Request.QueryString["ID"];


            if (auth.isLoggedIn() == true)
            {
                userGroup = int.Parse(HttpContext.Current.Session["benutzertyp"].ToString());
                allowedGroup = 2;
                if (auth.checkUserGroup(userGroup, allowedGroup) == false)
                {
                    Response.Redirect("~/Error.aspx");
                }
            }
            else
            {
                Response.Redirect("~/Error.aspx");
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            DataTable datatable = new DataTable();
            DataTable datatableRechte = new DataTable();
            TableRow row;
            TableCell cellButton;
            int numrows;
            int numcells;
            Button btnEdit;
            Button btnDel;
            DropDownList dd_rechte;
            TableCell cell;
            Connection mycon = new Connection();
            object[] values = null;
            ListItem option;

            datatableRechte= mycon.select("SELECT ID,Name FROM benutzertyp");
            datatable = mycon.select("SELECT b.kundennummer,b.vorname,b.nachname,b.email, r.Name, r.ID FROM benutzer as b Join benutzertyp as r on b.benutzertyp = r.ID ORDER BY b.kundennummer ASC");

            numrows = datatable.Rows.Count;

            for (int x = 0; x < numrows; x++)
            {
                values = datatable.Rows[x].ItemArray;
                numcells = datatable.Columns.Count;
                row = new TableRow();
               
                for (int i = 0; i < numcells-2; i++)
                {
                    cell = new TableCell();
                    cell.Controls.Add(new LiteralControl("" + values[i]));
                    row.Cells.Add(cell);
                }

                cellButton = new TableCell();
                dd_rechte = new DropDownList();
                for (int w = 0; w < datatableRechte.Rows.Count; w++)
                {
                    option = new ListItem();
                    option.Value = datatableRechte.Rows[w].ItemArray[0].ToString();
                    option.Text = datatableRechte.Rows[w].ItemArray[1].ToString();
                    dd_rechte.Items.Add(option);
                }
                dd_rechte.ID = "ddRechte_" + values[0];
                dd_rechte.Items.FindByValue(values[5].ToString()).Selected = true;
                dd_rechte.Width = 150;
                cellButton.Controls.Add(dd_rechte);
                row.Cells.Add(cellButton);

                cellButton = new TableCell();
                btnEdit = new Button();
                btnEdit.ID = "btnUpdate_" + values[0];
                btnEdit.Text = "Rechte ändern";
                btnEdit.CssClass = "btn btn-primary";
                btnEdit.Click += new System.EventHandler(btnSave_Click);
                cellButton.Controls.Add(btnEdit);
                row.Cells.Add(cellButton);

                cellButton = new TableCell();
                btnEdit = new Button();
                btnEdit.ID = "btnEdit_" + values[0];
                btnEdit.Text = "Passwort Zurücksetzen";
                btnEdit.CssClass = "btn btn-primary";
                btnEdit.Click += new System.EventHandler(btnSave_Click);
                cellButton.Controls.Add(btnEdit);
                row.Cells.Add(cellButton);

                cellButton = new TableCell();
                btnDel = new Button();
                btnDel.ID = "btnDel_" + values[0];
                btnDel.Text = "Löschen";
                btnDel.CssClass = "btn btn-danger";
                btnDel.Click += new System.EventHandler(btnSave_Click);
                cellButton.Controls.Add(btnDel);
                row.Cells.Add(cellButton);

                table1.Rows.Add(row);
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            DataTable datatable = new DataTable();
            Connection mycon = new Connection();
            Button clickedButton = (Button)sender;
            string randomwert = new Random().Next(1000000, 9999999).ToString();
            string selectedCity;

            
            if (clickedButton.ID.Split('_')[0] == "btnEdit")
            {
                //BEARBEITEN
                datatable= mycon.select("SELECT `email`,`vorname`,`nachname` FROM `benutzer` WHERE Kundennummer='" + clickedButton.ID.Split('_')[1] + "'");

                sendEmail mailtocustomer = new sendEmail("Noreply@0815.de", datatable.Rows[0].ItemArray[0].ToString() , "", "", "Passwort Zurueckgesetzt", "Hallo " + datatable.Rows[0].ItemArray[1].ToString() + " " + datatable.Rows[0].ItemArray[2].ToString() + ",<br> Ihr neues Passwort lautet: " + randomwert + " <br> Mit freundlichen Grüßen <br> Ihre 0815-IT GMBH");
                
                mycon.insertUpdate("UPDATE `benutzer` SET `passwort` =  '" + randomwert + "'  WHERE Kundennummer='" + clickedButton.ID.Split('_')[1] + "'");

            }
            else if (clickedButton.ID.Split('_')[0] == "btnUpdate")
            {
                selectedCity = ((DropDownList)benutzerDaten.FindControl("ddRechte_" + clickedButton.ID.Split('_')[1])).SelectedItem.Value;
                mycon.insertUpdate("UPDATE `benutzer` SET `benutzertyp` = '" + selectedCity + "' WHERE Kundennummer=" + clickedButton.ID.Split('_')[1] + "");
                Response.Redirect("~/Admin/Benutzer.aspx");
            }
            else
            {
                // DELETE BUTTON BENUTZER
                mycon.insertUpdate("DELETE FROM `software_verwaltung`.`benutzer` WHERE `benutzer`.`kundennummer` = '" + clickedButton.ID.Split('_')[1] + "'");
                Response.Redirect("~/Admin/Benutzer.aspx");
            }
        }
    }
}